package com.spyx.service.impl;

import com.atyx.spyx.enums.SkuType;
import com.atyx.spyx.model.product.Category;
import com.atyx.spyx.model.product.SkuInfo;
import com.atyx.spyx.model.search.SkuEs;
import com.atyx.spyx.vo.search.SkuEsQueryVo;
import com.spyx.activity.ActivityClient;
import com.spyx.auth.AuthContextHolder;
import com.spyx.mapper.ProductSearchMapper;
import com.spyx.product.ProductClient;
import com.spyx.service.ProductSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ProductSearchServiceImpl implements ProductSearchService {

    @Autowired
    private ProductSearchMapper productSearchMapper;

    @Autowired
    private ProductClient productClient;


    @Override
    public void uppersku(Long skuid) {
        System.out.println("上架的skuid="+skuid);
        //操作es-将skuEs的信息保存到es中
        //调取service-product项目中的主键查询方法-通过微服务调取OpenFeign
        //service-search调取service-product-client,service-product-client调取service-product
        SkuEs skuEs = new SkuEs(); //SkuEs中包含了SkuInfo和Category的信息
        //1.使用openFeign调取方法
        SkuInfo skuInfo = productClient.findById(skuid);
        //2.根据categoryId，查询category对象
        Category category = productClient.getcategorybyid(skuInfo.getCategoryId());
        //3.将查询后的结果赋值给SkuEs
        skuEs.setId(skuid);
        skuEs.setKeyword(skuInfo.getSkuName()+","+category.getName());//使用skuInfo的name+category的name
        if(skuInfo.getSkuType().longValue()== SkuType.COMMON.getCode()){
            //如果skuType值为普通
            skuEs.setSkuType(0);
            skuEs.setPrice(skuInfo.getPrice().doubleValue());
            skuEs.setStock(skuInfo.getStock());
            skuEs.setPerLimit(skuInfo.getPerLimit());
            skuEs.setSale(skuInfo.getSale());
        }else{
            //如果skuType值为秒杀

        }
        skuEs.setIsNewPerson(skuInfo.getIsNewPerson());
        skuEs.setCategoryId(skuInfo.getCategoryId());
        skuEs.setCategoryName(category.getName());
        skuEs.setImgUrl(skuInfo.getImgUrl());
        skuEs.setTitle(skuInfo.getSkuName());
        skuEs.setWareId(skuInfo.getWareId());
        //4.将封装后的skues保存到es中
        productSearchMapper.save(skuEs);
    }

    @Override
    public void downsku(Long skuid) {
        System.out.println("下架的skuid="+skuid);
        //操作es-从es中根据skuid删除SkuEs的信息
        productSearchMapper.deleteById(skuid);
    }

    @Override
    public List<SkuEs> getHotSkuList() {
        //查询es中的热销数据(根据hotScore进行降序查询)
        PageRequest pageablerequest = PageRequest.of(0, 10);//pageable的页码值是从0开始，显示第一页，10条数据
        Page<SkuEs> esPage = productSearchMapper.findByOrderByHotScoreDesc(pageablerequest);
        return esPage.getContent();
    }

    @Autowired
    private ActivityClient activityClient;
    @Override
    public Page<SkuEs> search(PageRequest pageRequest, SkuEsQueryVo queryVo) {
        //1.由于SkuEsQueryVo中有三个数据，categoryId和keyword来自于前端，wareId的值如何获取？？
        Long wareId = AuthContextHolder.getWareId();
        queryVo.setWareId(wareId);
        //2.筛选条件的组合
        // 有模糊查keyword值的：根据分类id+仓库id+模糊查
        // 没有模糊查keyword值: 根据分类id+仓库id检索
        Page<SkuEs> page=null;
        String keyword = queryVo.getKeyword();
        if(StringUtils.isEmpty(keyword)){
            //根据分类id+仓库id检索
           page= productSearchMapper.findByCategoryIdAndWareId(queryVo.getCategoryId(), queryVo.getWareId(), pageRequest);
        }else{
            //根据分类id+仓库id+模糊查
           page=productSearchMapper.findByCategoryIdAndWareIdAndKeyword(queryVo.getCategoryId(), queryVo.getWareId(), queryVo.getKeyword(), pageRequest);
        }
        //3.查询商品的活动规则
        //3.1 获取商品列表
        List<SkuEs> skuEsList = page.getContent();
        if(!CollectionUtils.isEmpty(skuEsList)){
            //3.2 从集合中获取商品id集合
            List<Long> skuidList = skuEsList.stream().map(skuEs -> skuEs.getId()).collect(Collectors.toList());
            //3.3 远程服务调取活动的信息service-activity模块的代码
            //3.3.1 先在service-activity模块中定义方法:根据skuidList查询每件商品的活动规则
            //3.3.2 调取方法,map中封装的就是每件商品及对应的优惠规则
            //key skuid, value  活动规则
            Map<Long,List<String>> map = activityClient.findactivityRule(skuidList);
            //3.4 将map中的活动规则取出，并赋值给Page中的skuEs
            if(map!=null){
                for (SkuEs skuEs : skuEsList) {
                    List<String> stringList = map.get(skuEs.getId().toString());
                    skuEs.setRuleList(stringList);
                }
            }
        }

        return page;
    }
}
